package org.yzh.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.sample.model.APIResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yzh.rbac.component.Constant;
import org.yzh.rbac.model.vo.Token;
import org.yzh.rbac.model.vo.User;
import org.yzh.rbac.service.UserService;

@Api(description = "认证API")
@RestController
public class AuthController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登陆")
    @PostMapping("auth/login")
    public APIResult<User> login(@ApiParam(value = "用户名", required = true) @RequestParam String username,
                                 @ApiParam(value = "密码", required = true) @RequestParam String password) {
        User user = userService.login(username, password);
        return new APIResult(user);
    }

    @GetMapping("system")
    public String system(@RequestAttribute(Constant.TokenName) Token token) {
        return token.getUsername() + "\taccess: system";
    }
}